package grafikon;

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.util.ArrayList;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Csüti
 */
public class gFrame extends JFrame {

    private gPanel p;                                           //My own class for the JPanel
    int steps = 10;                                             //Setup fot the scale
    JDialog fileChooserDialog = new JDialog();                  //Dialog for the filechooser
    JFileChooser fileChooser = new JFileChooser();              //FileChooser for saving image
    JMenuBar gFrameMenuBar = new JMenuBar();                    //The menu bar on the frame

    public gFrame(ArrayList<CoordsList> coords) {
        super.setTitle("Coordinate system");
        setResizable(true);

        //menubar felépítése
        JMenu fileMenu = new JMenu("File");
        JMenuItem saveImageMI = new JMenuItem("Save image");
        JMenuItem closeMI = new JMenuItem("Close");

        closeMI.addActionListener(new ActionListener() {                //Close menu item event

            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        saveImageMI.addActionListener(new ActionListener() {            //Save image menu item event

            @Override
            public void actionPerformed(ActionEvent e) {
                int returnVal = fileChooser.showSaveDialog(fileChooserDialog);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File imageFile = new File(fileChooser.getSelectedFile().getAbsolutePath());
                    if (!imageFile.getAbsolutePath().toLowerCase().endsWith(".jpeg")) {
                        imageFile = new File(imageFile.getAbsolutePath() + ".jpeg");
                    }
                    Boolean overWriteEnabled = true;
                    if (imageFile.exists()) {
                        int response = JOptionPane.showConfirmDialog(null,
                                "Felülírja a létező fájlt?", "A fájl már létezik",
                                JOptionPane.OK_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE);
                        if (response == JOptionPane.CANCEL_OPTION) {
                            overWriteEnabled = false;
                        }
                    }
                    if (overWriteEnabled) {
                        BufferedImage image = new BufferedImage(p.getWidth(), p.getHeight(), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g2 = image.createGraphics();
                        p.paint(g2);
                        try {
                            javax.imageio.ImageIO.write(image, "jpeg", imageFile);
                        } catch (IOException ex) {
                            Logger.getLogger(gFrame.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        });
        fileMenu.add(saveImageMI);
        fileMenu.add(new JSeparator());
        fileMenu.add(closeMI);
        gFrameMenuBar.add(fileMenu);

        //3 checkbox, show text, show cross, show net
        JMenu viewModeMenu = new JMenu("Appearance");
        final JMenuItem showCrossMI = new JCheckBoxMenuItem("Show cross");
        final JMenuItem showTextMI = new JCheckBoxMenuItem("Show text");
        final JMenuItem showNetMI = new JCheckBoxMenuItem("Show net");

        showCrossMI.setSelected(true);
        showTextMI.setSelected(true);
        showNetMI.setSelected(true);

        showCrossMI.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                p.setCrossEnabled(showCrossMI.isSelected());
            }
        });

        showTextMI.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                p.setShowTextEnabled(showTextMI.isSelected());
            }
        });

        showNetMI.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                p.setNetEnabled(showNetMI.isSelected());
            }
        });

        viewModeMenu.add(showCrossMI);
        viewModeMenu.add(showTextMI);
        viewModeMenu.add(showNetMI);
        gFrameMenuBar.add(viewModeMenu);
        setJMenuBar(gFrameMenuBar);

        fileChooserDialog.setMinimumSize(new java.awt.Dimension(400, 400));
        fileChooserDialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        fileChooserDialog.add(fileChooser);
        fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.getName().toLowerCase().endsWith(".jpeg");
            }

            @Override
            public String getDescription() {
                return "JPEG files";
            }
        });
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));


        p = new gPanel(steps, coords);

        p.addMouseWheelListener(new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                if (e.getWheelRotation() > 0) {
                    if (steps + 1 < 30) {
                        steps++;
                    }
                } else if (steps - 1 > 0) {
                    steps--;
                }

                p.setSteps(steps);
                p.repaint();
            }
        });

        this.setPreferredSize(new java.awt.Dimension(750, 800));
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        add(p);
        pack();

    }
}
